<?php

namespace app\common\repositories\activity;


use app\common\dao\activity\NewActivityProductDao;
use app\common\repositories\BaseRepository;
use think\facade\Db;

class NewActivityProductRepository extends BaseRepository
{
    public function __construct(NewActivityProductDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * @param array $where
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: fupeng
     * @day: 2024/1/31
     */
    public function search(?int $merId,array $where, $page, $limit)
    {
        $query = $this->dao->search($where,$merId)->with([
        'product' => function ($query) {
            $query->field('product_id,store_name,stock,price,sales,status,is_used,mer_status,is_show')->append(['us_status']);
        },
        'newActivity' => function ($query) {
            $query->field('new_activity_id,title');
        },
        ])->order('NewActivityProduct.create_time DESC');
        $count = $query->count($this->dao->getPk());
        $list = $query->page($page, $limit)->select();
        return compact('count', 'list');
    }

    public function getAdminList($where,$page,$limit)
    {
        $query = $this->dao->search($where)->field('NewActivityProduct.*')->with([
            'product' => function ($query) {
                $query->append(['us_status']);
            },
            'newActivity' => function($query){
                $query->field('new_activity_id,title');
            },
            'merchant' => function($query){
                $query->field('mer_id,mer_name');
            },
        ])->order('NewActivityProduct.create_time DESC');

        $count = $query->count();
        $list = $query->page($page,$limit)->select();
        $list->append([ 'us_status']);
        return compact('count','list');
    }



    /**
     * @param array $where
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: fupeng
     * @day: 2024/1/31
     */
    public function searchAll(array $where,$field = '*',$order = 'create_time DESC')
    {
        $query = $this->dao->search($where)->order($order);
        $count = $query->count($this->dao->getPk());
        $list = $query->field($field)->select();
        return compact('count', 'list');
    }

    /**
     * @param int $id
     * @param $except
     * @return bool
     * @author: fupeng
     * @day: 2024/1/31
     */
    public function exists(int $id, $except = null)
    {
        return $this->dao->fieldExists($this->dao->getPk(), $id, $except);
    }

    public function switchStatus($id,$data)
    {
        return $this->dao->update($id,$data);
    }




}
